package com.example.exam.staff.controller;

import com.alibaba.fastjson.JSON;
import com.example.exam.authorization.service.AuthorizationService;
import com.example.exam.common.ResultBean;
import com.example.exam.common.ResultBeanCodeEnum;
import com.example.exam.db.PageInfoBean;
import com.example.exam.post.model.PostBean;
import com.example.exam.post.service.PostService;
import com.example.exam.staff.model.StaffBean;
import com.example.exam.staff.service.StaffService;
import com.example.exam.user.model.UserBean;
import com.example.exam.user.model.UserRoleBean;
import com.example.exam.user.service.UserRoleService;
import com.example.exam.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    StaffService staffService;
    @Autowired
    UserService userService;
    @Autowired
    PostService postService;
    @Autowired
    AuthorizationService authorizationService;
    @Autowired
    UserRoleService userRoleService;
    private static String base_path="/staff/";
    @GetMapping("/toStaff")
        /**
            * @Description:去员工信息页面
            * @Param: [request]
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String toStaff(HttpServletRequest request){
        HttpSession session=request.getSession();
        Integer userId=(Integer) session.getAttribute("id");
        /*List<UserRoleBean> list=userRoleService.authorization(userId);
        if(list!=null){
            for (int i=0;i<list.size();i++){
                if("SysStaff|toEdit".equals(list.get(i).getPre())){
                    request.setAttribute("update",list.get(i));
                }else if("SysStaff|toDelete".equals(list.get(i).getPre())){
                    request.setAttribute("delete",list.get(i));
                }else if("SysStaff|toGrant".equals(list.get(i).getPre())){
                    request.setAttribute("grant",list.get(i));
                }else if ("SysStaff|toAdd".equals(list.get(i).getPre())){
                    request.setAttribute("add",list.get(i));
                }
            }
        }*/
        Integer id=(Integer)session.getAttribute("id");
        String post=authorizationService.findPost(id);
        if (post!=null){
            if("八岗".equals(post)||"七岗".equals(post)){
                post="八岗";
            }else{
                post=null;
            }
        }
        Integer num=authorizationService.findRole(id);
        request.setAttribute("post",post);
        request.setAttribute("num",num);
        return base_path+"staff";
    }
    @GetMapping("/toAdd")
        /**
            * @Description:去添加员工页面
            * @Param: [request]
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String toAdd(HttpServletRequest request){
        List<UserBean> list=userService.listUser();
        List<PostBean> list2=postService.listPost();
        request.setAttribute("list",list);
        request.setAttribute("list2",list2);
        return base_path+"add";
    }
    @GetMapping("/toEdit")
        /**
            * @Description:去修改员工信息页面
            * @Param: [id, request]
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String toEdit(Integer id, HttpServletRequest request){
        StaffBean staffBean=staffService.findById(id);
        List<PostBean> list2=postService.listPost();
        request.setAttribute("list2",list2);
        request.setAttribute("staffBean",staffBean);
        return base_path+"edit";
    }
    /**
     * @Description:添加员工信息
     * @Param: [staffBean]
     * @return: com.example.exam.common.ResultBean
     * @Author: xhw
     * @Date: 2021/7/26
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultBean add(StaffBean staffBean,HttpServletRequest request){
        ResultBean resultBean=new ResultBean();
        try{
            Integer i=staffService.add(staffBean);
            Integer id=staffService.findByName(staffBean.getName());
            Integer[] ids=new Integer[1];
            if (staffBean.getPost()>1){
                ids[0]=2;
                userRoleService.save(ids,staffBean.getUser(),request,false);
            }else{
                ids[0]=8;
                userRoleService.save(ids,staffBean.getUser(),request,false);
            }
            Integer count=staffService.addStaffUser(id,staffBean.getUser());
            userService.editType(staffBean.getUser(),-1);
            if(i>0){
                resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }else{
                resultBean.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
                resultBean.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
            }
        }catch(Exception e){
            resultBean.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
            e.fillInStackTrace();
        }
        return resultBean;
    }
    @PostMapping("/delete")
    @ResponseBody
        /**
            * @Description:删除员工信息
            * @Param: [id]
            * @return: com.example.exam.common.ResultBean
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public ResultBean delete(Integer id,HttpServletRequest request){
        ResultBean resultBean=new ResultBean();
        try{
            Integer i=staffService.delete(id);
            Integer userId=staffService.findByUserId(id).getUser();
            userService.editType(userId,1);
            Integer[] ids=new Integer[1];
            ids[0]=8;
            userRoleService.save(ids,userId,request,false);
            staffService.deleteStaffUser(id);
            if(i>0){
                resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }else{
                resultBean.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
                resultBean.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
            }
        }catch(Exception e){
            resultBean.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
            e.fillInStackTrace();
        }
        return resultBean;
    }
    @PostMapping("/edit")
    @ResponseBody
        /**
            * @Description:编辑员工信息
            * @Param: [staffBean]
            * @return: com.example.exam.common.ResultBean
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public ResultBean edit(StaffBean staffBean,HttpServletRequest request){
        ResultBean resultBean=new ResultBean();
        try{
            Integer i=staffService.edit(staffBean);
            Integer id=staffService.findUserId(staffBean.getId());
            Integer[] ids=new Integer[1];
            if (staffBean.getPost()>1){
                ids[0]=2;
                userRoleService.save(ids,id,request,false);
            }else{
                ids[0]=8;
                userRoleService.save(ids,id,request,false);
            }
            if(i>0){
                resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }else{
                resultBean.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
                resultBean.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
            }
        }catch(Exception e){
            resultBean.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
            e.fillInStackTrace();
        }
        return resultBean;
    }
    @PostMapping("/list")
    @ResponseBody
        /**
            * @Description:员工信息列表
            * @Param: [page, limit, name]
            * @return: com.example.exam.common.ResultBean
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public ResultBean list(Integer page,Integer limit,String name){
        ResultBean resultBean = new ResultBean();
        try {
            PageInfoBean pageInfo = staffService.list(page,limit,name);
            resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            resultBean.fillData(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            resultBean.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
        }
        return resultBean;
    }
    @RequestMapping("/toDelete")
    @ResponseBody
        /**
            * @Description:员工信息批量删除
            * @Param: [request]
            * @return: com.example.exam.common.ResultBean
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public ResultBean toDelete(HttpServletRequest request) {
        ResultBean result = new ResultBean();
        String role_id  = request.getParameter("id");
        //System.out.println("ids"+role_id);
        try {
            Integer deleState = null;
            if(StringUtils.isNotEmpty(role_id)) {
                int[] roleIds = null;
                String[] ids = role_id.split(",");
                roleIds = new int[ids.length];
                for(int i=0;i<ids.length;i++){
                    roleIds[i]=Integer.parseInt(ids[i]);
                }
                for(int i = 0; i < roleIds.length; i++) {
                    deleState = staffService.delete(roleIds[i]);
                }
                result.fillData(deleState);
                result.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }else {
                result.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
            }
            result.fillData(deleState);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            result.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
        }
        return result;
    }
}
